package wh;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;
import wh.guistuff.MyLogTable;
import wh.iolists.ProcessedOrderList;

public class LogDialog extends JDialog {
	
	private static final String[] columns = {"OrderID", "Customer", "Quantity", "Cost", "Started", "Ended"};
	
	public LogDialog(JFrame frame, boolean modal, ProcessedOrderList orders)
	{
		super(frame, modal);
		
		createGUI(orders);
	}
	
	private void createGUI(ProcessedOrderList proOrders)
	{
		DefaultTableModel model = new DefaultTableModel();
		
		for (int i = 0; i < columns.length; i++) {
			model.addColumn(columns[i]);
		}
		
		Vector<String> rowData = null;
    	
    	for (int i = 0; i < proOrders.getPOrderListSize(); i++)
    	{
    		ProcessedOrder order = proOrders.getProcessedOrder(i);
    		
    		rowData = new Vector<String>();
      		rowData.addElement(order.getOrderID());
      		rowData.addElement(order.getCustomerName());
      		rowData.addElement(""+order.getQuantity());
      		rowData.addElement(""+order.getTotalCost());
      		rowData.addElement(order.getStartDate().toString());
      		rowData.addElement(order.getEndDate().toString());
      		
      		model.addRow(rowData);
    	}
    	
    	FlowLayout flow = new FlowLayout();
    	flow.setAlignment(FlowLayout.CENTER);
    	flow.setHgap(4);
    	flow.setVgap(4);
		
		MyLogTable logTable = new MyLogTable(model);
		
		JScrollPane scroll = new JScrollPane(logTable);
		scroll.setPreferredSize(new Dimension(760, 410));
		
		JPanel panel = new JPanel(flow);
		panel.setBackground(Color.white);
		panel.add(scroll);
		
		JButton exitButton = new JButton("Exit Log!");
		exitButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	
            	setVisible(false);
            	dispose();
            }
    	});
		panel.add(exitButton);
		
		getContentPane().add(BorderLayout.CENTER, panel);
	}
}
